package com.chromatic.competition.view.home;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chromatic.competition.R;
import com.chromatic.competition.databinding.FragmentHomeBinding;

public class HomeFragment extends Fragment {
    private FragmentHomeBinding binding;
    private WebView myWebView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private boolean isRefreshing = false; // 添加一个标记来表示是否正在刷新

    @SuppressLint("SetJavaScriptEnabled")
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        // Get the WebView from the binding
        myWebView = root.findViewById(R.id.webview_home);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setDomStorageEnabled(true);
        // 启用 WebView 的缩放控件
        myWebView.getSettings().setBuiltInZoomControls(true);
        // 可选，这将显示或隐藏缩放控件
        myWebView.getSettings().setDisplayZoomControls(false);
        // 使网页内容自适应屏幕大小
        myWebView.getSettings().setLoadWithOverviewMode(true);
        // 页面将以宽视图端口模式加载
        myWebView.getSettings().setUseWideViewPort(true);
        myWebView.setFocusable(true);
        myWebView.setFocusableInTouchMode(true);

        myWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(request.getUrl().toString());
                return true;
            }
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                isRefreshing = false; // 刷新完成后将标记设置为 false，允许下次刷新
                swipeRefreshLayout.setRefreshing(false); // 停止刷新状态
            }
        });
        myWebView.loadUrl("https://contest-mobile.fzuenactus.org.cn");

        // Setup SwipeRefreshLayout
        swipeRefreshLayout = root.findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (!isRefreshing) {
                    isRefreshing = true; // 设置正在刷新的标记为 true
                    myWebView.reload();
                }
            }
        });

        ImageView backButton = root.findViewById(R.id.backButton);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });

        return root;
    }

    private void goBack() {
        if (myWebView.canGoBack()) {
            myWebView.goBack();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}